package dhm.com.hpx.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.utils.SpUtils;

public class AccountActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.quan)
    ImageView quan;
    @BindView(R.id.def_no)
    ImageView defNo;
    @BindView(R.id.def_yes)
    ImageView defYes;
    @BindView(R.id.text_phone)
    TextView textPhone;
    private boolean isDef = false;
    private String uid;

    private PressenterImpl pressenter;

    @Override
    protected int getLayout() {
        return R.layout.activity_account;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        uid = SpUtils.getString(this, "uid");
        String phone = SpUtils.getString(this, "phone");
        String str2 = phone.substring(0, 3) + "******" + phone.substring(9);
        textPhone.setText(str2);
        showPopwindow();

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.back, R.id.quan, R.id.logout, R.id.xieyi, R.id.yinsi})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.xieyi:
                intent = new Intent(this, WebActivity.class);
                intent.putExtra("type","1");
                startActivity(intent);
                break;
            case R.id.yinsi:
                intent = new Intent(this, WebActivity.class);
                intent.putExtra("type","2");
                startActivity(intent);
                break;
            case R.id.quan:
                if (isDef) {
                    quan.setBackgroundResource(R.drawable.back_quan);
                    defNo.setVisibility(View.VISIBLE);
                    defYes.setVisibility(View.GONE);
                } else {
                    quan.setBackgroundResource(R.drawable.back_quans);
                    defYes.setVisibility(View.VISIBLE);
                    defNo.setVisibility(View.GONE);
                }
                isDef = !isDef;
                break;
            case R.id.logout:
                setBackgroundAlpha(0.5f);
                popWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
                break;
            default:
        }
    }



    private PopupWindow popWindow;

    private void showPopwindow() {
        View popView = View.inflate(this, R.layout.dialog_exit, null);
        TextView cancle = popView.findViewById(R.id.cancle);
        TextView title = popView.findViewById(R.id.title);
        TextView sure = popView.findViewById(R.id.sure);
        title.setText("注销账户后会清除您所有数据，如采集、推广，如果您是会员会清除会员不回退，请谨慎注销");
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("uid",uid);
                pressenter.sendMessage(AccountActivity.this,Constant.cancellation_user,map, Bean.class);

                popWindow.dismiss();
            }
        });

        popWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popWindow.setFocusable(true);
        popWindow.setTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.setOutsideTouchable(true);// 设置允许在外点击消失


        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // popupWindow隐藏时恢复屏幕正常透明度
                setBackgroundAlpha(1.0f);
            }
        });

    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha 屏幕透明度0.0-1.0 1表示完全不透明
     */
    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }


    @Override
    public void requesta(Object data) {
        if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (((Bean) data).getCode()==1){
                SpUtils.clear(AccountActivity.this);
                Intent intent = new Intent(AccountActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                SpUtils.saveString(AccountActivity.this,"firstcom","1");
                finish();
            }

        }
    }

    @Override
    public void fail(String error) {

    }
}
